﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GraphDrawer.GraphProvider;

namespace GraphDrawer
{
    public partial class GenerateNetwork : UserControl
    {
        ArrayOfString parametersTypes;
        public Graph graph;

        public GenerateNetwork()
        {
            InitializeComponent();
            generators.IsEnabled = false;
            Description.Text = Resource.GenerateNetworkDescription;
            Menu.graphProvider.GetAvailableMethodsOfGeneratorCompleted += new EventHandler<GraphDrawer.GraphProvider.GetAvailableMethodsOfGeneratorCompletedEventArgs>(graphProvider_GetAvailableMethodsOfGeneratorCompleted);
            Menu.graphProvider.GenerateGraphCompleted += new EventHandler<GenerateGraphCompletedEventArgs>(graphProvider_GenerateGraphCompleted);
            Menu.graphProvider.GetParametersTypesFromMethodCompleted += new EventHandler<GraphDrawer.GraphProvider.GetParametersTypesFromMethodCompletedEventArgs>(graphProvider_GetParametersTypesFromMethodCompleted);
            Menu.graphProvider.GetParametersFromMethodCompleted += new EventHandler<GetParametersFromMethodCompletedEventArgs>(graphProvider_GetParametersFromMethodCompleted);
            Menu.graphProvider.GetAvailableMethodsOfGeneratorAsync();
        }

        void graphProvider_GetParametersFromMethodCompleted(object sender, GetParametersFromMethodCompletedEventArgs e)
        {
            ArrayOfString parameters = e.Result;
            panel.Children.Clear();
            foreach (var p in parameters)
            {
                StackPanel sp = new StackPanel()
                {
                    Orientation = Orientation.Horizontal,
                    Margin = new Thickness(0, 10, 0, 0)
                };
                sp.Children.Add(new TextBox()
                {
                    Width = 100
                });
                sp.Children.Add(new TextBlock()
                {
                    Text = GetParameterDescription(generators.SelectedItem.ToString(), p)
                });

                panel.Children.Add(sp);
            }

            this.Height = 150.0 + 35.0 * e.Result.Count;
        }
		
		string GetParameterDescription(string method, string p) {
			Type res = typeof(Resource);
            string description = (string)res.GetProperty(method + "_" + p, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static).GetValue(null, null);
			return description;
		}
		
        void graphProvider_GenerateGraphCompleted(object sender, GenerateGraphCompletedEventArgs e)
        {
            graph = e.Result;
            //TODO: remove this
            foreach (var item in graph.Vertices)
            {
                PairKeyValue val = new PairKeyValue();
                val.Key = "stateName";
                val.Value = "Susceptible";
                item.Keys.Add(val);
            }
            Menu.PopUp.btnOk.IsEnabled = true;
        }

        void graphProvider_GetParametersTypesFromMethodCompleted(object sender, GraphDrawer.GraphProvider.GetParametersTypesFromMethodCompletedEventArgs e)
        {
            parametersTypes = new ArrayOfString();
            foreach (var item in e.Result)
                parametersTypes.Add(item);
        }

        void graphProvider_GetAvailableMethodsOfGeneratorCompleted(object sender, GraphDrawer.GraphProvider.GetAvailableMethodsOfGeneratorCompletedEventArgs e)
        {
            foreach (string item in e.Result)
                generators.Items.Add(item);

            generators.IsEnabled = true;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (generators.SelectedItem != null)
            {
                string methodName = generators.SelectedItem.ToString();
                ArrayOfAnyType parameters = new ArrayOfAnyType();

                int j = 0;
                for (int i = 0; i < panel.Children.Count; i++)

                    switch (parametersTypes[j++])
                    {
                        case "System.Int32":
                            parameters.Add(int.Parse(((panel.Children[i] as StackPanel).Children[0] as TextBox).Text));
                            break;
                        case "System.Double":
                            parameters.Add(double.Parse(((panel.Children[i] as StackPanel).Children[0] as TextBox).Text));
                            break;
                        default:
                            break;
                    }


                Menu.graphProvider.GenerateGraphAsync(methodName, parameters);
            }
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (generators.SelectedItem != null)
            {
                string methodName = generators.SelectedItem.ToString();
                Menu.graphProvider.GetParametersFromMethodAsync(methodName);
                Menu.graphProvider.GetParametersTypesFromMethodAsync(methodName);
            }
        }
    }
}
